<template>
	<view class="beijingse">
		<header-top :messageInfo="{typeFlag:true,message:titletext,liner:false}"></header-top>
		<view class="mainpadding">
			<view class="flexbetween1">
				<view class="titletext">收货人</view>
				<input class="inputs" type="text" placeholder="请输入收货人姓名" v-model="name">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">手机号码</view>
				<input class="inputs" type="text" placeholder="请输入收货人手机号" v-model="phone">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">选择地址</view>
				<!-- <view class="flexbetween xiahuaxian">
					<input class="inputs1" type="text" placeholder="选择省、市、县" v-model="address">
					<view class="huiyoujt"></view>
				</view> -->
				<view class="flexbetween xiahuaxian">
					<uni-data-picker  @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区" v-model="area" 
					class="zdy"
						>
						<text class="inputs1" v-if="!area.length" style="color: #999999;">请选择住址</text>
						<text class="inputs1" v-else>{{ area[0] }}/{{ area[1] }}/{{ area[2] }}</text>
					</uni-data-picker>
					<view class="huiyoujt"></view>
				</view>
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">详细地址</view>
				<input class="inputs" type="text" placeholder="街道、楼牌号等" v-model="addressDetail">
			</view>
			<view class="flexbetween1 margin_top2">
				<view class="titletext">设为默认地址</view>
				<view class="">
					<u-switch space="2" v-model="value11" activeColor="#FD463E" inactiveColor="rgb(230, 230, 230)">
					</u-switch>
				</view>
			</view>
		</view>
		<!-- 固定底部 -->
		<view class="gudingdb" @click="tzxzshdz">
			<view class="bigbtn">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "../../static/city/city.js"
	export default {
		data() {
			return {
				titletext: "新增收货地址",
				listid: "",
				value11: false,
				name: '',
				phone: '',
				addressDetail: '',
				area: [], //收获地址
				address: {
					province: "", //省
					city: "", //市
					county: "", //区
				},
				items: citys.data,
			}
		},
		onLoad(options) {
			this.listid = options.id;
			if (this.listid) {
				this.titletext = "编辑收货地址"
			} else {
				this.titletext = "新增收货地址"
			}
		},
		onShow() {
			this.dizhixq()
		},
		methods: {
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			// 修改地址接口
			xiugdz(){
				httpRequest.request('api/user/editAddress', 'POST', {
					// 传参
					id: this.listid,
					name: this.name,
					mobile: this.phone,
					address_detail: this.addressDetail,
					status: this.value11 ? 1 : 0,
					province_id: this.address.province,
					city_id: this.address.city,
					area_id: this.address.county
				}, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			// 地址详情接口
			dizhixq() {
				httpRequest.request('api/user/addressDetail', 'POST', {
					// 传参
					id: this.listid
				}, false, false, true).then(res => {
					if (res.code == 1) {
						// this.dzlist = res.data
						this.area = res.data.cityinfo.split("/")
						this.address.province = res.data.province_id
						this.address.city = res.data.city_id
						this.address.county = res.data.area_id
						this.name = res.data.name
						this.phone = res.data.mobile
						this.addressDetail = res.data.address_detail
						if (res.data.status == 1) {
							this.value11 = true
						} else {
							this.value11 = false
						}
					}
				})
			},
			// 添加地址
			tzxzshdz() {
				uni.showLoading({
					title: '请稍候...',
					mask:true
				})
				let _this = this;
				if (_this.name == '') {
					uni.hideLoading()
					httpRequest.toast('请输入名字');	
					return false;
				}
				if (_this.phone == "") {
					uni.hideLoading()
					httpRequest.toast('请输入手机号');
					return false;
				}
				if (!this.isMobile(this.phone)) {
					uni.hideLoading()
					httpRequest.toast("手机号输入有误")
					return false
				}
				if (_this.area == false) {
					uni.hideLoading()
					httpRequest.toast('请选择地址');
					return false;
				}
				if (_this.addressDetail == false) {
					uni.hideLoading()
					httpRequest.toast('请输入详细地址');
					return false;
				}
				if(this.listid){
					this.xiugdz()
				}else{
					httpRequest.request('api/user/addAddress', 'POST', {
						// 传参
						name: this.name,
						mobile: this.phone,
						address_detail: this.addressDetail,
						status: this.value11 ? 1 : 0,
						province_id: this.address.province,
						city_id: this.address.city,
						area_id: this.address.county
					}, false, false, true).then(res => {
						uni.hideLoading()
						httpRequest.toast(res.msg)
						if (res.code == 1) {
							setTimeout(() => {
								uni.navigateBack(1)
							}, 1000)
						}
					})
				}
				
			},
			bindPickerChange(val) {
				let area = val.detail.value
				this.area = [area[0].text, area[1].text, area[2].text];
				this.address.province = area[0].value;
				this.address.city = area[1].value;
				this.address.county = area[2].value;
			},
			submit() {
				let _this = this;
				httpRequest.request('')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.xiahuaxian {
		width: 72%;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #F1F1F1;
	}
	.zdy{
		width: 100%;
	}

	.flexbetween1 {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		padding: 10rpx 0;
	}

	.inputs1 {
		width: 100%;
	}

	.inputs {
		width: 72%;
		padding-bottom: 20rpx;
		border-bottom: 2rpx solid #F1F1F1;
	}
</style>
